package com.weishu.banking.controller;

import com.weishu.common.core.web.controller.BaseController;
import com.weishu.common.core.web.domain.AjaxResult;
import com.weishu.common.core.web.page.TableDataInfo;
import com.weishu.common.security.annotation.PreAuthorize;
import com.weishu.core.entity.JrRentInstallment;
import com.weishu.core.entity.JrSfLoan;
import com.weishu.core.entity.vo.JrRentInstallmentVo;
import com.weishu.core.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/finance")
public class FinanceController extends BaseController {
    @Autowired
    FinanceService financeService;
    @Resource
    private IJrSfLoanService iJrSfLoanService;
    @Resource
    private ICfContractService iCfContractService;

    @Resource
    private IJrProductService iJrProductService;
    @Resource
    private IJrRentInstallmentService iJrRentInstallmentService;


    @PreAuthorize(hasPermi = "banking:finance:list")
    @GetMapping("/list")
    public TableDataInfo list(JrRentInstallmentVo jrRentInstallmentVo)
    {
        startPage();
        List<JrRentInstallmentVo> list = financeService.searchLoans(jrRentInstallmentVo);
        return getDataTable(list);
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    //@PreAuthorize(hasPermi = "business:contract:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(iCfContractService.selectTCfContractById(id));
    }

    @RequestMapping("rentingAuditView")
    public AjaxResult rentingAuditView(JrRentInstallmentVo jrRentInstallment){

        ModelAndView mv =new ModelAndView();
        Long business=null;
        // 金融产品通过jrProductType判断 跳转不同页面：   1:租房分期  2:收房贷  3:应收租约保理
        if(jrRentInstallment.getJrProductType()==1){

        }else if(jrRentInstallment.getJrProductType()==2){
            //获取收房贷详细信息
            JrSfLoan tJrSfLoan = iJrSfLoanService.selectSfLoanDetails(Long.valueOf(jrRentInstallment.getId()));
//            System.out.println("1++++++++"+tJrSfLoan);
            BigDecimal loanAmount = tJrSfLoan.getLoanAmount();
            //贷款利息
            BigDecimal divide = loanAmount.divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_HALF_UP);
            BigDecimal multiply = divide.multiply(tJrSfLoan.getRate());
            tJrSfLoan.setReserve1(multiply);
            //一次性服务费
            BigDecimal divide1 = loanAmount.divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_HALF_UP);
            BigDecimal multiply1 = divide.multiply(tJrSfLoan.getServiceFee());
            tJrSfLoan.setReserve2( multiply1);
            //周期还款金额
            BigDecimal divide2 = multiply.divide(BigDecimal.valueOf(tJrSfLoan.getMonthLimit()), 2, BigDecimal.ROUND_HALF_UP);
            BigDecimal divide3= multiply1.divide(BigDecimal.valueOf(tJrSfLoan.getMonthLimit()),2,BigDecimal.ROUND_HALF_UP);
            BigDecimal add = divide2.add(divide3);
            tJrSfLoan.setReserve3(String.valueOf(add));
            //获取收房信息
//            TSfContract =
            return  AjaxResult.success(tJrSfLoan);
        }else {

        }

        return  AjaxResult.success(mv);
    }

    /**
     * 修改收房贷申请信息
     */
    //@PreAuthorize(hasPermi = "houses:loan:edit")
//    @Log(title = "收房贷申请信息", businessType = BusinessType.UPDATE)
    @PutMapping("updateById")
    public int edit(@RequestBody JrRentInstallment shenhe)
    {System.out.println(shenhe);
        System.err.println(Long.valueOf(shenhe.getId()));
        JrRentInstallment jrRentInstallment = iJrRentInstallmentService.selectJrRentInstallment(Long.valueOf(shenhe.getId()));
        System.err.println(jrRentInstallment);
        jrRentInstallment.setApplyState(shenhe.getApplyState());
        return iJrRentInstallmentService.updateJrRentInstallment(jrRentInstallment);
    }


}
